<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">出库单编号</label>
        <el-input v-model.trim="query.outBoundOrderCode" clearable placeholder="出库单编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" @clear="crud.toQuery" />
        <label class="el-form-item-label">客户</label>
        <el-input v-model.trim="query.customer" clearable placeholder="客户" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" @clear="crud.toQuery" />
        <label class="el-form-item-label">出库单状态</label>
        <el-select v-model="query.orderStatus" class="filter-item" clearable placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in dict.allOrder_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">出库时间</label>
        <el-date-picker
          v-model="query.startTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          class="filter-item"
          @change="crud.toQuery"
        />
        <label class="el-form-item-label">计算状态</label>
        <el-select v-model="query.calculateStatus" class="filter-item" clearable filterable placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in dict.calculate_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">执行类型</label>
        <el-input v-model.trim="query.executeType" clearable placeholder="执行类型" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" @clear="crud.toQuery" />
        <!-- <label class="el-form-item-label">出库单类型</label>
        <el-input v-model="query.outBoundType" clearable placeholder="出库单类型" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <rrOperation :crud="crud" />
      </div>
      <crudOperation :permission="permission">
        <el-button slot="right" type="primary" size="mini" class="filter-item" @click="createOrder">创建出库单</el-button>
        <el-button slot="right" type="primary" size="mini" class="filter-item" @click="tailBox">尾数箱</el-button>
      </crudOperation>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="outBoundOrderCode" label="出库单编号" />
        <el-table-column prop="outBoundType" label="出库单类型" />
        <el-table-column prop="customer" label="客户" />
        <el-table-column prop="calculateStatus" label="计算状态">
          <template slot-scope="scope">
            {{ dict.label.calculate_status[scope.row.calculateStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="executeType" label="执行类型" />
        <el-table-column prop="orderStatus" label="出库单状态">
          <template slot-scope="scope">
            {{ dict.label.allOrder_status[scope.row.orderStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="appointmentTallyStatus" label="预约理货状态">
          <template slot-scope="scope">
            {{ dict.label.appointmentTally_status[scope.row.appointmentTallyStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="startTime" label="出库时间" />
        <el-table-column prop="endTime" label="结束时间" />
        <el-table-column prop="isNightStockUp" label="是否晚间备货">
          <template slot-scope="scope">
            {{ dict.label.isNightStockUp[scope.row.isNightStockUp] }}
          </template>
        </el-table-column>
        <el-table-column prop="isAppointmentTally" label="是否需要预约理货">
          <template slot-scope="scope">
            {{ dict.label.isAppointmentTally[scope.row.isAppointmentTally] }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="updateTime" label="更新时间" />
        <el-table-column prop="prepareDate" label="发货时间" />
        <el-table-column label="操作" width="430px" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="checkDetail(scope.row)">查看详情</el-button>
            <!-- <el-button type="warning" size="mini" @click="manualOperation(scope.row)">人工操作</el-button> -->
            <el-popconfirm
              confirm-button-text="确定"
              cancel-button-text="取消"
              icon="el-icon-info"
              icon-color="red"
              title="点击此按钮,将执行该出库单,是否继续"
              @confirm="executeOrder(scope.row.id, scope.$index)"
            >
              <el-button slot="reference" type="warning" size="mini" :loading="btn.excute === scope.$index">执行出库单</el-button>
            </el-popconfirm>
            <el-button size="mini" type="success" :loading="btn.continua === scope.$index" @click="continuaOrder(scope.row, scope.$index)">继续</el-button>
            <el-button size="mini" type="warning" :loading="btn.pause === scope.$index" @click="pauseOrder(scope.row, scope.$index)">暂停</el-button>
            <el-popconfirm
              confirm-button-text="确定"
              cancel-button-text="取消"
              icon="el-icon-info"
              icon-color="red"
              title="点击此按钮将进行预约理货,是否继续"
              @confirm="appointmentTally(scope.row, scope.$index)"
            >
              <el-button slot="reference" type="primary" :loading="btn.appointmentTally === scope.$index" size="mini">执行预约理货</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      <!-- 抽屉控件 -->
      <!-- <el-drawer
        title="详情"
        :visible.sync="drawer"
        :direction="direction"
        :before-close="handleDrawerClose"
        size="25%"
      >
        <div style="display: flex;flex-direction: column;border: 1px solid #efefef;height: auto;align-self: flex-start;box-sizing: border-box;">
          <div style="background-color: #efefef;padding: 15px;">出库单编号</div>
          <div style="padding: 15px;">{{ itemDetail.outBoundOrderCode }}</div>
          <div style="background-color: #efefef;padding: 15px;">出库单类型</div>
          <div style="padding: 15px;">{{ itemDetail.outBoundType }}</div>
          <div style="background-color: #efefef;padding: 15px;">计算状态</div>
          <div style="padding: 15px;">{{ dict.label.calculate_status[itemDetail.calculateStatus] }}</div>
          <div style="background-color: #efefef;padding: 15px;">执行类型</div>
          <div style="padding: 15px;">{{ itemDetail.executeType }}</div>
          <div style="background-color: #efefef;padding: 15px;">出库单状态</div>
          <div style="padding: 15px;">{{ dict.label.allOrder_status[itemDetail.orderStatus] }}</div>
          <div style="background-color: #efefef;padding: 15px;">预约理货状态</div>
          <div style="padding: 15px;">{{ dict.label.appointmentTally_status[itemDetail.appointmentTallyStatus] }}</div>
          <div style="background-color: #efefef;padding: 15px;">是否需要预约理货</div>
          <div style="padding: 15px;">{{ dict.label.isAppointmentTally[itemDetail.isAppointmentTally] }}</div>
          <div style="background-color: #efefef;padding: 15px;">出库时间</div>
          <div style="padding: 15px;">{{ itemDetail.startTime }}</div>
          <div style="background-color: #efefef;padding: 15px;">结束时间</div>
          <div style="padding: 15px;">{{ itemDetail.endTime }}</div>
          <div style="background-color: #efefef;padding: 15px;">任务操作</div>
          <div style="padding: 15px;display: flex;flex-direction: column;gap: 5px;">
            <el-button type="info" style="height: 5vh;font-size: medium;" :loading="btn.force">执行出库单</el-button>
            <el-button type="primary" style="height: 5vh;font-size: medium;" :loading="btn.edit">执行预约理货</el-button>
            <el-button type="success" style="height: 5vh;font-size: medium;">继续作业</el-button>
            <el-button type="warning" style="height: 5vh;font-size: medium;" :loading="btn.cancel">暂停作业</el-button>
          </div>
        </div>
      </el-drawer> -->
    </div>
  </div>
</template>

<script>
import { executeOrderAPI, appointmentTallyAPI, continuaOrderAPI, pauseOrderAPI } from '@/api/work/outBoundOrder'
import crudOutBoundOrder from '@/api/work/outBoundOrder'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/Pagination'
import crudOperation from '@crud/CRUD.operation'

const defaultForm = { id: null, outBoundOrderCode: null, executionOrder: null, orderStatus: null, outBoundType: null, createTime: null, startTime: null, endTime: null, createName: null, updateName: null, isNightStockUp: null, isAppointmentTally: null }
export default {
  name: 'OutBoundOrder',
  components: { pagination, rrOperation, crudOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'outBound', url: 'api/productOutBoundOrder', idField: 'id', sort: 'id,desc', crudMethod: { ...crudOutBoundOrder },
      optShow: {
        add: false,
        reset: true,
        edit: false,
        download: true,
        del: false
      }
    })
  },
  dicts: ['allOrder_status', 'calculate_status', 'appointmentTally_status', 'isNightStockUp', 'isAppointmentTally'],
  data() {
    return {
      adjustVisible: false,
      formLabelWidth: '120px',
      form: {},
      options: [],
      permission: {
        add: ['admin', 'outBoundOrder:add', 'otherUser'],
        edit: ['admin', 'outBoundOrder:edit', 'otherUser'],
        del: ['admin', 'outBoundOrder:del', 'otherUser']
      },
      rules: {
      },
      btn: {
        calculate: false,
        excute: '',
        appointmentTally: '',
        continua: '',
        pause: ''
      },
      drawer: false,
      direction: 'rtl',
      itemDetail: {}
    }
  },
  mounted() {
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    async manualOperation(row) {
      this.itemDetail = row
      this.drawer = true
    },
    handleDrawerClose() {
      this.drawer = false
    },
    async appointmentTally(row, index) {
      // console.log(index)
      const submitForm = {
        outBoundId: row.id
      }
      try {
        this.btn.appointmentTally = index
        const res = await appointmentTallyAPI(submitForm)
        if (res.result === 'OK') {
          this.$message.success('执行成功')
          this.crud.toQuery()
        } else {
          this.$message.warning(res.data)
        }
      } finally {
        this.btn.appointmentTally = ''
      }
    },
    async executeOrder(val, index) {
      try {
        this.btn.excute = index
        const res = await executeOrderAPI({
          outBoundId: val
        })
        if (res.result === 'OK') {
          this.$message.success('执行成功')
          this.crud.toQuery()
        } else {
          this.$message.warning(res.data)
        }
      } finally {
        this.btn.excute = ''
      }
    },
    checkDetail(row) {
      this.$router.push(`/outBoundManagement/outBoundOrderDetail?outBoundId=${row.id}&outBoundOrderCode=${row.outBoundOrderCode}`)
    },
    createOrder() {
      this.$router.push('/outBoundManagement/outBoundCreate')
    },
    handlerAdjust(row) {
      this.adjustVisible = true
      this.form.firstOrder = row.outBoundOrderCode
    },
    handleClose() {
      this.adjustVisible = false
    },
    tailBox() {
      this.$router.push('/outBoundManagement/tailBox')
    },
    async continuaOrder(row, index) {
      console.log(row)
      try {
        this.btn.continua = index
        // setTimeout(() => {
        //   this.btn.continua = ''
        // }, 2000)
        const res = await continuaOrderAPI({
          outBoundId: row.id
        })
        console.log(res)
        if (res.result === 'OK') {
          this.crud.toQuery()
          this.$message.success(res.message)
        } else {
          this.$message.warning(res.data)
        }
      } finally {
        this.btn.continua = ''
      }
    },
    async pauseOrder(row, index) {
      console.log(row)
      try {
        this.btn.pause = index
        // setTimeout(() => {
        //   this.btn.pause = ''
        // }, 2000)
        const res = await pauseOrderAPI({
          outBoundId: row.id
        })
        console.log(res)
        if (res.result === 'OK') {
          this.crud.toQuery()
          this.$message.success(res.message)
        } else {
          this.$message.warning(res.data)
        }
      } finally {
        this.btn.pause = ''
      }
    }
  }
}
</script>

<style scoped>

</style>
